<?php
namespace App\Http\Controllers\Api;

use App\Http\Resources\Api\GroupCourseResource;
use App\Models\GroupCourse;
use Carbon\Carbon;
use Illuminate\Http\Request;

class GroupCourseController extends Controller
{
    public function index(Request $request){
        $day = $request->day;
        $date = date('Y') . '-' . str_replace('.', '-', $day);
        $weekday = Carbon::parse($date)->weekday();
        $weekday = $weekday + 1;
        $courses = GroupCourse::where(['is_enable' => true])
            ->where(function($query) use ($weekday, $date) {
                $query->where('week', $weekday)->orWhereDate('date', $date);
            })
            ->orderByDesc('created_at')
            ->get();
        return api()->success(GroupCourseResource::collection($courses));
    }

    public function show($id)
    {
        $course = GroupCourse::findOrFail($id);
        return api()->success(new GroupCourseResource($course));
    }
}
